#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void* thread1(void *arg){
   pthread_detach(pthread_self());
   printf("%s\n", (char*)arg);
   return NULL;
}

void* thread2(void* arg){
  while(1){
    printf("i am thread2\n");
    sleep(2);
  }
  return NULL;
}

int main(){
  pthread_t tid;
  char arg[] = "i am thread1";
  if (pthread_create(&tid, NULL, thread1, arg) != 0){ 
    printf("create thread error\n");
    return 1;
  }
  int ret = 0;
  //sleep(1);//很重要，要让线程先分离，再等待
  if ( pthread_join(tid, NULL ) == 0  ) {
    printf("pthread wait success\n");
    ret = 0;
  } else {
    printf("pthread1 wait failed\n");
    ret = 1;
  }
  pthread_create(&tid, NULL, thread2, NULL);
  pthread_detach(tid);
  if ( pthread_join(tid, NULL ) == 0  ) {
    printf("pthread wait success\n");
    ret = 0;
  } else {
    printf("pthread2 wait failed\n");
    ret = 1;
  }
  return ret;
}
